#!/usr/bin/perl

use strict;

if ( ! @ARGV ) {
    if ( -e "$ENV{'HOME'}/.wall.pid" ) {
        print "Daemon running, please enter argument for client use\n";
        exit;
    }

    require Wallpaper::Loader;

} else {
    use IO::Socket;
    my $command = $ARGV[0];
    my $sock = new IO::Socket::UNIX(
                                   Peer      => '/tmp/wallpaper_socket',
                                   Type      => SOCK_STREAM,
                                   Timeout   => 10
                               ) or die "Could not create socket: $!\n";

    socket_write( $sock, $command );
    
    my $response = socket_read( $sock );
    print "$response";

    sub socket_read {
        my ( $socket ) = @_;
        my $data;

        while(<$socket>) {
            last if ( $_ eq "END\n" );
            $data .= $_;
        }
    return $data;
    }

    sub socket_write {
        my ( $socket, $out ) = @_;

        print $socket "$out\n";
    }

    exit;

}
